/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import jeeobserver.ActionObserver;
import jeeobserver.Observer;
import jeeobserver.utilities.TimeUtilities;

/**
 * The Class ActionNotificationRule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class ActionNotificationRule extends ActionExclusionRule {

	 /** The Constant serialVersionUID. */
	 private static final long serialVersionUID = 40L;

	 /** The Constant TAG_CODE_CONTEXT. */
	 public static final String TAG_CODE_CONTEXT = "{context}";

	 /** The Constant TAG_CODE_PROJECT. */
	 public static final String TAG_CODE_PROJECT = "{project}";

	 /** The Constant TAG_CODE_CATEGORY. */
	 public static final String TAG_CODE_CATEGORY = "{category}";

	 /** The Constant TAG_CODE_PRINCIPAL. */
	 public static final String TAG_CODE_PRINCIPAL = "{principal}";

	 /** The Constant TAG_CODE_NAME. */
	 public static final String TAG_CODE_NAME = "{name}";

	 /** The Constant TAG_CODE_START_TIMESTAMP. */
	 public static final String TAG_CODE_START_TIMESTAMP = "{start_timestamp}";

	 /** The Constant TAG_CODE_CPU_NANOS. */
	 public static final String TAG_CODE_CPU_NANOS = "{cpu_nanos}";

	 /** The Constant TAG_CODE_CPU_FORMATTED. */
	 public static final String TAG_CODE_CPU_FORMATTED = "{cpu_formatted}";

	 /** The Constant TAG_CODE_TIME_NANOS. */
	 public static final String TAG_CODE_TIME_NANOS = "{time_nanos}";

	 /** The Constant TAG_CODE_TIME_FORMATTED. */
	 public static final String TAG_CODE_TIME_FORMATTED = "{time_formatted}";

	 /** The Constant TAG_CODE_EXCEPTION_MESSAGE. */
	 public static final String TAG_CODE_EXCEPTION_MESSAGE = "{exception_message}";

	 /** The Constant TAG_CODE_EXCEPTION_CLASS. */
	 public static final String TAG_CODE_EXCEPTION_CLASS = "{exception_class}";

	 /** The Constant TAG_CODE_EXCEPTION_STACK_TRACE. */
	 public static final String TAG_CODE_EXCEPTION_STACK_TRACE = "{exception_stack_trace}";

	 /** The notification. */
	 private Notification notification;

	 /**
	  * Instantiates a newaction notification rule.
	  *
	  * @param notification the notification
	  */
	 public ActionNotificationRule(Notification notification) {
			this.notification = notification;
	 }

	 /**
	  * Gets the notification.
	  *
	  * @param observer theaction observer
	  *
	  * @return the notification
	  */
	 public Notification getNotification(ActionObserver observer) {

			//Remove all tags filled before.
			this.notification.removeTags();

			if (observer != null) {
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_CONTEXT, observer.getContext());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_PROJECT, observer.getProject());

				 String categoryName = "-";
				 switch (observer.getCategory()) {
						case ActionObserver.CATEGORY_SERVLET:
							 categoryName = "Servlet";
							 break;
						case ActionObserver.CATEGORY_JSF:
							 categoryName = "JSF";
							 break;
						case ActionObserver.CATEGORY_EJB:
							 categoryName = "EJB";
							 break;
						case ActionObserver.CATEGORY_CUSTOM:
							 categoryName = "Custom";
							 break;
				 }

				 this.notification.setTag(ActionNotificationRule.TAG_CODE_CATEGORY, categoryName);
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_NAME, observer.getName());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_PRINCIPAL, observer.getPrincipal());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_START_TIMESTAMP, observer.getDate());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_CPU_NANOS, observer.getCpuNanos());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_TIME_NANOS, observer.getTimeNanos());
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_CPU_FORMATTED, TimeUtilities.fromNanosToString(observer.getCpuNanos()));
				 this.notification.setTag(ActionNotificationRule.TAG_CODE_TIME_FORMATTED, TimeUtilities.fromNanosToString(observer.getTimeNanos()));

				 if (observer.getException() != null) {
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_CLASS, observer.getException().getType());
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_MESSAGE, observer.getException().getMessage());
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_STACK_TRACE, observer.getException().getStackTrace());
				 } else {
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_CLASS, "");
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_MESSAGE, "");
						this.notification.setTag(ActionNotificationRule.TAG_CODE_EXCEPTION_STACK_TRACE, "");
				 }
			}

			return this.notification;
	 }

	 /**
	  * Gets the notification.
	  *
	  * @return the notification
	  */
	 public Notification getNotification() {
			return this.notification;
	 }

	 /**
	  * Sets the notification.
	  *
	  * @param notification the new notification
	  */
	 public void setNotification(Notification notification) {
			this.notification = notification;
	 }
}
